Acala

Row

Overall Total Value Locked (TVL)

  • The Overall TVL in the table below does not includes the Crowdloan TVL, which also seems to be the case on the stats.Acala.network page.
Date Block dexTVL bridgeTVL liquidStakingTVL crowdloanTVL LPTokenStakingTVL lcdotTVL stableCoinTVL OverallTVL
2022-02-10 382,658 0 4,166,200,000 0.00 677,340,384 12,859,946 502,363,724 7,341,784 4,688,765,454
2022-02-11 389,478 16,094,236 3,783,000,000 0.00 615,039,766 16,981,773 456,157,162 17,382,712 4,289,615,883
2022-02-12 396,298 17,016,890 3,766,800,000 0.00 612,405,972 17,499,952 454,203,753 18,165,975 4,273,686,569
2022-02-13 403,080 16,954,608 3,740,600,000 0.00 608,146,378 17,607,851 451,044,536 18,378,380 4,244,585,374
2022-02-14 409,851 16,790,368 3,752,200,000 0.00 610,032,305 17,696,564 452,443,273 18,606,109 4,257,736,314
2022-02-15 416,618 17,201,570 4,027,800,000 0.00 654,839,326 17,829,240 485,675,341 22,576,976 4,571,083,128
2022-02-16 423,364 19,156,534 3,994,800,000 0.00 649,474,189 20,650,471 481,696,175 25,420,112 4,541,723,292
2022-02-17 429,431 18,765,765 3,633,800,000 0.00 590,782,845 19,849,904 438,166,507 24,404,858 4,134,987,033
2022-02-18 435,379 18,569,217 3,564,400,000 0.00 579,499,800 19,835,430 429,798,199 24,337,215 4,056,940,062
2022-02-19 441,244 18,878,530 3,567,600,000 0.00 580,020,055 19,740,153 430,184,058 24,803,661 4,061,206,402
2022-02-20 447,046 19,023,121 3,426,800,000 0.00 557,128,805 19,973,860 413,206,281 30,189,557 3,909,192,820
2022-02-21 452,805 20,067,041 3,249,000,000 0.00 528,222,099 20,531,419 391,767,015 30,621,759 3,711,987,234
2022-02-22 458,591 19,701,249 3,265,400,000 0.00 530,888,409 20,302,879 393,744,540 30,963,722 3,730,112,390
2022-02-23 464,375 19,537,419 3,212,600,000 0.00 522,304,190 19,901,682 387,377,874 30,455,664 3,669,872,640
2022-02-24 470,299 18,494,709 3,193,200,000 0.00 519,150,140 19,256,956 385,038,607 30,264,104 3,646,254,376
2022-02-25 476,519 19,319,181 3,448,400,000 0.00 560,640,531 20,215,913 415,810,826 32,588,262 3,936,334,182
2022-02-26 483,026 19,450,986 3,600,600,000 0.00 585,385,192 20,814,561 434,163,224 34,170,399 4,109,199,170
2022-02-27 489,670 19,361,671 3,486,200,000 0.00 566,786,051 20,421,553 420,368,781 33,081,046 3,979,433,051
2022-02-28 496,214 19,375,345 3,758,400,000 0.00 611,040,301 21,454,201 453,190,874 35,851,414 4,288,271,834
2022-03-01 502,741 19,797,112 37,738,000 0.00 613,544,031 21,846,985 455,047,818 36,068,245 570,498,160
2022-03-02 509,328 19,714,554 37,294,000 0.00 606,325,483 21,809,479 449,694,031 35,875,337 564,387,401
2022-03-03 515,915 21,601,531 36,096,000 0.00 586,848,411 22,767,995 435,248,451 62,551,692 578,265,670
2022-03-04 522,577 21,109,933 33,526,000 0.00 545,065,377 21,782,568 404,259,186 62,448,822 543,126,509
2022-03-05 529,288 21,046,495 34,594,000 0.00 562,428,910 22,337,883 417,137,215 65,629,199 560,744,792
2022-03-06 535,942 21,517,523 34,130,000 0.00 554,885,203 22,085,828 411,542,266 64,244,858 553,520,476
2022-03-07 542,760 20,698,881 32,756,000 0.00 532,546,724 21,700,357 394,974,464 61,806,180 531,935,883
2022-03-08 549,653 21,426,517 33,786,000 0.00 549,292,454 22,176,760 407,394,287 62,804,874 547,588,438
2022-03-09 556,567 22,187,048 35,432,000 0.00 576,053,106 22,948,291 427,241,886 67,566,345 575,375,570
2022-03-10 563,674 21,797,345 34,072,000 0.00 553,942,239 22,297,969 410,842,898 64,484,996 553,495,208
2022-03-11 570,668 22,623,490 35,698,000 0.00 580,377,731 22,701,836 430,449,335 65,532,242 577,004,904
2022-03-12 577,693 23,152,676 36,542,000 0.00 594,099,475 23,793,862 440,626,355 67,263,520 591,378,413
2022-03-13 584,752 22,686,501 34,726,000 0.00 564,574,965 23,082,152 418,728,882 64,046,341 563,269,876
2022-03-14 591,860 22,701,522 35,342,000 0.00 574,589,887 23,759,257 426,156,659 69,433,042 577,392,480
2022-03-15 598,871 23,036,986 35,672,000 0.00 579,955,023 23,646,099 430,135,825 67,957,199 580,448,109
2022-03-16 605,882 23,921,422 37,984,000 0.00 617,543,497 24,473,523 458,014,106 71,306,499 615,699,551
2022-03-17 612,917 22,161,063 37,460,000 0.00 609,024,310 22,408,882 451,695,672 70,199,287 603,924,904
2022-03-18 620,006 22,438,611 38,024,000 0.00 618,193,816 22,816,787 458,496,429 72,350,849 614,126,677
2022-03-19 627,056 22,353,285 38,728,000 0.00 629,639,441 23,107,153 466,985,317 75,180,260 626,354,016
2022-03-20 634,100 22,073,101 37,390,000 0.00 607,886,250 22,613,059 450,851,607 73,690,440 606,618,206
2022-03-21 641,175 22,291,249 37,496,000 0.00 609,609,597 22,855,693 452,129,763 77,140,785 611,913,491
2022-03-22 648,244 23,278,446 40,526,000 0.00 658,871,307 24,075,593 488,665,745 78,792,353 655,338,137
2022-03-23 655,070 23,461,611 41,664,000 0.00 677,372,900 24,188,169 502,387,840 81,244,807 672,946,427
2022-03-24 661,932 21,153,952 42,586,000 0.00 692,362,767 21,782,899 513,505,390 83,431,449 682,459,690
2022-03-25 668,922 21,306,124 41,092,000 0.00 668,073,330 21,459,150 495,490,618 79,385,632 658,733,524
2022-03-26 675,883 21,542,172 41,658,000 0.00 677,275,352 21,883,262 502,315,491 82,516,037 669,914,963
2022-03-27 682,919 23,079,423 44,528,000 0.00 723,935,784 23,294,929 536,922,181 86,241,201 714,065,733
2022-03-28 689,974 23,409,978 44,534,000 0.00 724,033,332 23,605,668 536,994,529 87,643,336 716,187,512
2022-03-29 697,050 23,591,321 44,294,000 0.00 720,131,414 23,675,358 534,100,590 89,331,651 714,992,920
2022-03-30 704,068 23,857,058 45,036,000 0.00 732,194,843 24,050,814 543,047,685 92,283,577 728,275,134
2022-03-31 711,104 23,395,737 42,972,000 0.00 698,638,351 23,720,373 518,159,808 96,421,301 704,669,220
2022-04-01 718,140 23,701,108 43,878,000 0.00 713,368,090 24,315,051 529,084,429 104,794,678 725,773,266
2022-04-02 725,135 24,887,333 45,910,000 0.00 746,404,326 25,420,495 553,586,447 110,536,827 760,341,102
2022-04-03 732,169 24,976,394 46,504,000 0.00 756,061,572 25,676,763 560,748,947 109,398,237 767,304,341
2022-04-04 739,232 24,915,365 45,238,000 0.00 735,478,957 25,766,211 545,483,417 114,607,746 756,010,740
2022-04-05 746,312 24,464,193 44,110,000 0.00 717,139,944 25,349,733 531,881,903 112,332,392 738,138,221
2022-04-06 753,339 23,335,329 39,880,000 0.00 648,368,646 23,577,454 480,876,225 98,378,537 666,047,546
2022-04-07 760,400 23,441,333 40,698,000 0.00 661,667,682 23,987,465 490,739,735 104,512,956 683,379,489
2022-04-08 767,454 23,329,475 38,616,000 0.00 627,818,546 23,211,996 465,634,812 97,358,875 648,151,160
2022-04-09 774,447 23,552,480 39,236,000 0.00 637,898,500 23,724,436 473,110,822 102,638,645 662,262,382
2022-04-10 781,545 23,609,472 39,172,000 0.00 636,857,989 24,104,843 472,339,105 108,925,810 668,151,229
2022-04-11 788,553 22,497,741 34,518,000 0.00 561,193,303 22,129,748 416,220,801 95,815,282 591,181,572
2022-04-12 795,620 22,429,761 35,566,000 0.00 578,231,676 22,377,307 428,857,669 95,523,923 604,754,660
2022-04-13 802,608 22,989,411 36,390,000 0.00 591,628,260 22,896,487 438,793,527 99,176,405 620,245,829
2022-04-14 809,644 23,399,894 35,830,000 0.00 582,523,786 22,857,382 432,041,002 96,065,859 610,194,137
2022-04-15 816,668 23,280,552 36,614,000 0.00 595,270,050 22,919,057 441,494,537 98,171,487 622,479,633
2022-04-16 823,732 23,117,546 37,270,000 0.00 605,935,292 23,330,616 449,404,637 103,152,932 636,275,732
2022-04-17 830,766 23,167,619 35,584,000 0.00 578,524,320 22,884,413 429,074,714 98,611,872 609,322,619
2022-04-18 837,826 23,230,902 36,382,000 0.00 591,498,196 23,022,673 438,697,062 95,405,181 616,737,818
2022-04-19 844,870 23,931,360 37,750,000 0.00 613,739,127 23,872,544 455,192,515 98,350,575 639,096,995
2022-04-20 851,796 24,071,604 38,196,000 0.00 620,990,190 23,866,194 460,570,419 97,073,865 643,778,082
2022-04-21 858,830 23,832,440 36,368,000 0.00 591,270,585 23,481,671 438,528,249 94,872,285 617,082,645
2022-04-22 865,842 24,221,077 36,652,000 0.00 595,887,854 23,563,965 441,952,744 92,765,978 619,155,764
2022-04-23 872,830 24,054,342 37,652,000 0.00 612,145,844 23,786,452 454,010,823 91,038,073 630,541,691
2022-04-24 879,807 23,898,657 36,488,000 0.00 593,221,543 23,181,231 439,975,218 85,704,047 609,247,154
2022-04-25 886,715 23,237,058 36,294,000 0.00 590,067,493 23,032,354 437,635,951 86,257,029 606,456,393
2022-04-26 893,713 22,897,882 33,242,000 0.00 540,448,107 21,606,459 400,834,691 79,312,452 557,893,484
2022-04-27 900,680 22,972,638 33,832,000 0.00 550,040,322 21,905,632 407,948,958 80,635,836 567,295,065
2022-04-28 907,463 22,683,419 33,962,000 0.00 552,153,860 21,915,225 409,516,509 82,101,429 570,178,582
2022-04-29 914,420 21,562,000 32,254,000 0.00 524,385,213 21,083,198 388,921,308 76,937,998 540,758,504
2022-04-30 921,375 20,113,100 29,036,000 0.00 472,067,001 19,512,552 350,118,407 70,866,527 489,646,586
2022-05-01 928,262 20,315,368 30,704,000 0.00 499,185,329 19,960,327 370,231,284 72,475,060 513,686,040
2022-05-02 935,210 20,209,506 29,986,000 0.00 487,512,092 19,649,879 361,573,583 70,661,182 502,080,151
2022-05-03 941,991 20,104,425 29,408,000 0.00 478,114,973 19,671,442 354,604,013 71,190,161 494,978,041
2022-05-04 948,773 20,989,523 32,496,000 0.00 528,319,647 20,874,165 391,839,364 79,907,714 546,106,766
2022-05-05 955,553 19,749,098 28,805,352 0.00 472,424,677 19,364,033 350,383,685 70,122,361 488,424,529
2022-05-06 962,314 19,359,434 28,322,787 18,709.66 465,953,997 18,999,861 345,584,569 69,951,596 482,236,956
2022-05-07 969,145 18,932,729 27,319,892 18,116.83 447,289,824 18,671,415 331,741,894 95,920,777 492,604,824
2022-05-08 975,941 17,839,603 26,412,065 17,508.28 432,104,861 17,846,015 320,479,647 89,520,323 472,115,160
2022-05-09 982,741 14,690,241 21,844,115 14,556.12 356,342,627 15,245,813 264,288,995 76,471,410 392,555,129
2022-05-10 989,571 13,635,935 31,470,849 10,573,849.39 366,422,581 13,477,134 271,765,004 80,548,328 421,471,100
2022-05-11 996,410 10,259,516 39,693,269 25,248,761.36 289,662,107 10,458,617 214,833,986 75,631,240 376,125,389
2022-05-12 1,003,195 9,581,486 43,924,635 29,452,097.77 280,460,084 9,670,162 208,009,113 75,134,974 375,772,467
2022-05-13 1,010,207 10,973,168 53,902,003 37,477,184.73 340,279,733 10,995,529 252,375,612 89,176,105 454,899,602
2022-05-14 1,017,179 11,331,616 59,848,280 42,070,697.82 371,267,462 11,615,082 275,358,312 98,681,018 498,905,006
2022-05-15 1,024,222 11,916,291 61,742,528 43,613,104.21 381,022,256 11,963,073 282,593,160 103,845,945 515,674,101
2022-05-16 1,031,247 11,070,042 56,676,320 40,172,009.19 347,303,185 11,216,298 257,584,702 95,712,866 472,432,238
2022-05-17 1,038,263 11,549,278 59,421,133 42,267,121.91 359,626,741 11,588,136 266,724,727 98,506,448 490,056,844
2022-05-18 1,044,985 11,505,734 52,688,111 37,631,336.97 313,434,540 11,567,356 232,465,310 85,493,128 431,350,976
2022-05-19 1,051,890 11,467,898 55,006,948 39,959,639.25 325,582,510 11,531,299 241,475,108 85,772,675 445,213,566
2022-05-20 1,058,796 11,246,893 54,112,456 39,615,645.46 316,347,972 11,313,775 234,626,118 84,278,962 435,193,850
2022-05-21 1,065,696 11,187,309 55,754,295 41,060,888.51 325,354,898 11,328,144 241,306,294 86,987,403 447,624,333
2022-05-22 1,072,674 11,445,206 57,322,456 42,003,535.27 331,370,355 11,587,782 245,767,784 89,726,681 457,853,445
2022-05-23 1,079,644 11,393,589 56,031,571 41,420,150.22 323,244,611 11,404,036 239,741,156 86,373,238 446,363,741
2022-05-24 1,086,605 11,650,983 59,641,714 44,612,795.50 331,500,418 11,741,444 245,864,249 92,542,067 466,053,252
2022-05-25 1,093,548 11,355,388 58,284,123 43,547,582.16 321,238,375 11,509,233 238,253,189 89,923,289 452,872,804
2022-05-26 1,100,467 10,677,864 54,650,843 41,107,060.91 300,376,122 10,733,398 222,780,261 83,262,272 423,211,698
2022-05-27 1,107,371 10,335,111 54,492,214 41,115,076.23 297,059,492 10,405,127 220,320,412 81,623,140 418,291,081
2022-05-28 1,114,253 10,864,004 57,829,255 43,749,969.61 314,331,981 11,062,521 233,130,916 85,553,245 442,189,911
2022-05-29 1,121,139 11,131,725 59,670,533 45,136,471.59 324,051,007 11,370,286 240,339,236 88,774,150 456,422,402
2022-05-30 1,128,033 11,732,215 63,097,953 47,756,791.78 342,263,208 11,934,351 253,846,698 97,769,331 486,137,339
2022-05-31 1,134,939 11,805,351 74,274,661 58,963,766.59 340,474,829 11,948,136 252,520,309 96,860,090 506,372,314
2022-06-01 1,141,677 11,244,679 67,690,554 53,678,288.38 308,339,286 11,313,703 228,686,308 87,774,716 460,388,248
2022-06-02 1,148,531 11,450,050 71,169,404 56,511,651.37 322,851,168 11,618,434 239,449,350 92,935,143 483,134,033
2022-06-03 1,155,361 11,086,517 67,146,049 53,381,288.38 304,756,025 11,136,768 226,028,707 86,257,482 455,036,812
2022-06-04 1,162,191 11,044,275 67,871,496 53,877,894.95 305,695,737 11,231,847 226,725,664 87,782,839 458,534,016
2022-06-05 1,168,945 11,222,834 68,035,148 54,202,573.86 305,464,873 11,272,468 226,554,440 87,558,038 458,845,501
2022-06-06 1,175,760 11,275,119 68,894,536 54,781,007.72 307,841,791 11,394,874 228,317,331 88,813,285 463,476,153
2022-06-07 1,182,503 11,038,517 67,628,096 53,779,095.36 299,810,344 11,120,725 222,360,639 87,255,444 453,182,515
2022-06-08 1,189,273 10,853,966 66,630,434 53,132,601.89 293,245,368 10,869,771 217,491,587 85,246,943 444,225,303
2022-06-09 1,196,022 10,910,543 68,656,497 54,610,401.78 299,664,022 10,935,414 222,252,117 86,688,056 454,053,028
2022-06-10 1,202,808 10,455,392 64,562,776 51,566,393.28 281,442,067 10,516,651 208,737,421 81,255,048 427,093,681
2022-06-11 1,209,560 9,902,483 60,176,361 48,054,853.09 261,847,937 9,959,983 194,205,023 75,615,459 397,914,162
2022-06-12 1,216,309 9,303,464 56,264,446 44,898,748.56 244,692,506 9,353,530 181,481,337 69,012,028 370,313,554
2022-06-13 1,222,967 8,759,431 51,599,685 41,109,628.30 224,441,554 8,756,378 166,461,793 64,169,610 340,856,525
2022-06-14 1,229,621 8,848,993 54,864,329 43,817,449.90 238,524,225 8,956,648 176,906,501 67,628,085 361,022,007
2022-06-15 1,236,320 9,764,121 63,184,240 50,421,654.82 274,265,790 9,768,169 203,414,984 75,271,580 411,824,749
2022-06-16 1,242,989 8,920,166 53,176,197 42,246,468.86 229,458,769 8,918,006 170,182,916 64,113,318 347,557,073
2022-06-17 1,249,647 9,157,904 55,272,154 43,888,309.03 238,029,982 9,118,175 176,539,936 65,960,543 359,937,021
2022-06-18 1,256,310 8,640,015 53,475,500 42,513,907.14 229,595,336 8,686,825 170,284,204 63,353,334 346,953,785
2022-06-19 1,263,003 8,885,295 57,035,607 45,131,272.59 243,544,692 8,969,960 180,630,037 66,981,819 367,633,991
2022-06-20 1,269,614 8,720,955 59,515,435 47,121,220.35 253,933,548 8,717,673 188,335,150 70,367,150 382,777,584
2022-06-21 1,276,376 8,808,088 59,155,268 46,820,285.73 251,904,550 8,769,325 186,830,301 70,704,699 381,087,966
2022-06-22 1,283,096 8,572,605 57,026,894 45,133,825.07 242,487,923 8,550,627 179,846,261 67,528,621 366,658,833
2022-06-23 1,289,783 8,728,083 59,378,994 47,089,747.52 252,525,606 8,794,238 187,290,920 70,574,437 381,856,419
2022-06-24 1,296,569 9,012,942 63,041,858 50,080,246.65 268,777,093 9,077,803 199,344,176 74,295,241 404,852,267
2022-06-25 1,303,311 8,936,434 62,037,923 49,338,164.37 264,016,753 8,991,273 195,813,571 72,511,923 397,629,287
2022-06-26 1,310,012 8,882,814 60,378,693 48,013,815.78 256,616,116 8,844,341 190,324,733 70,964,000 387,408,397
2022-06-27 1,316,693 8,813,879 60,027,917 47,754,506.10 254,522,087 8,810,507 188,771,652 70,618,722 384,797,183
2022-06-28 1,321,626 8,614,478 58,378,521 46,471,829.67 247,137,708 8,595,677 183,294,872 68,009,498 373,364,876

Row

Components of Total Value Locked

Row

Sources

Please click on the Source Code link at the top of the document to see the full source code. Here is a summary:

dexTVL

  • sum of tvlUSD from the xx function in the subscanr package for R. You can also use the Karura-dex project hosted on Subquery Network with this query:
{ 
  dailyPools { 
    nodes { 
      timestamp token0 {id} token1 {id} feeRateUSD dailyTradeVolumeUSD totalTVL txCount updateAtBlock {id}  
    } 
  } 
}

LPTokenStakingTVL

  • ACA (or KAR) obtained from the py-substrate-interface using function substrate.query(module=‘Rewards’,storage_function=‘PoolInfos’, block_hash = hash)
  • mulitplied by ACA (or KAR) price for the specific block number

BridgeTVL

  • KSM (or DOT) obtained from the py-substrate-interface using function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘Token’: ‘KSM’}], block_hash = hash)
  • LKSM (or LDOT) obtained from function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘Token’: ‘LKSM’}], block_hash = hash)
  • both mulitplied by KSM (or DOT) price for the specific block number

liquidStakingTVL

  • KSM (or DOT) obtained from the py-substrate-interface using function substrate.query(module=‘Homa’,storage_function=‘TotalStakingBonded’, block_hash = hash)
  • mulitplied by KSM (or DOT) price for the specific block number

lcdotTVL (Acala only)

  • LCDOT obtained from the py-substrate-interface using function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘LiquidCrowdloan’: 13}], block_hash = hash)
  • mulitplied by DOT price for the specific block number

stableCoinTVL

{ 
  dailyCollaterals { 
    nodes { 
      collateral {id} depositAmount debitAmount depositVolumeUSD debitVolumeUSD
      depositChangedUSD debitChangedUSD debitExchangeRate timestamp txCount 
    } 
  } 
}
---
title: "Acala / Karura TVL Dashboard"
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    social: menu
    source_code: embed
params:
  network: Karura
  window: 7
  
---

```{css custom1, echo=FALSE}
.dataTables_scrollBody {
    max-height: 100% !important;
}
```

```{r global, include=FALSE}
library(knitr)
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)

library(ggplot2)
library(kableExtra)
library(formattable)
library(lubridate)
library(flexdashboard)
library(DT)
library(subscanr)
library(formattable)
library(ghql)
x <- GraphqlClient$new()

# Helper function to concat
`%+%` <- function(a, b) paste0(a, b)

window = params$window
# window <- today() - as.Date("2021-12-31") + 1
network = params$network

# Liquidity Pool TVL ($3.72M) = totalDefiTvl = totalDexTvl + totalDexLockedTvl + homaLocked + stableCoinTvl
dex <- getDailyPools_acala_dex(network, window) %>%
  setorder(Date, pair)

dailyTVL <- dex[, .(max(updateAtBlock.id), sum(tvlUSD)), by = Date] %>%
  setnames(c("V1","V2"), c("Block","dexTVL"))

library(reticulate)
# use_python("/opt/homebrew/bin/python3.9")
```

```{python, include=FALSE}
from substrateinterface import SubstrateInterface

import pandas as pd

def getTotalIssuanceKSM():
    url = 'wss://karura.polkawallet.io'
    substrate = SubstrateInterface(url)
    hash = substrate.get_block_hash(block_id)
    timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
  
    # Issuance
    ksm = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'KSM'}], block_hash = hash)
    lksm = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LKSM'}], block_hash = hash)
    result = substrate.query('AcalaOracle', 'Values', params = [{'Token': 'KSM'}], block_hash = hash)
  
    # homa = substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)
    try:
        homa = substrate.query_map(module='Homa',storage_function='StakingLedgers', block_hash = hash)
        amount = 0
        for res in homa:
            amount += res[1].value['bonded']
            
        # LKSM to KSM ratio
        ratio = lksm.value / amount 

    except:
        amount = 0
        # LKSM to KSM ratio
        ratio = 8.5
    
    # Build price DataFrame
    p = substrate.query('AcalaOracle', 'Values', params = [{'Token': 'KSM'}], block_hash = hash)
    price = p.value['value'] / 10**18
  
    bridge = (float(ksm.value / 10**12) + float(lksm.value / 10**12 / ratio)) * price
    liquidKSM = float(amount / 10**12) * price
    crowdloanTVL = (501137661910050505 / 10**12) * price
    return [bridge, liquidKSM, crowdloanTVL, block_id, ratio]

def getOraclePrices():
    if r.network=="Karura":
        url = 'wss://karura.polkawallet.io'
    else:
        url = 'wss://acala-rpc-0.aca-api.network'
      
    substrate = SubstrateInterface(url)
    hash = substrate.get_block_hash(block_id)
    timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
    p = substrate.query_map('AcalaOracle', 'Values', block_hash = hash)
    data = []
    for res in p:
        outi = {"token": str(res[0].value), "price": res[1].value['value'] / 10**18}
        data.append(outi)
    
    out = pd.DataFrame(data)
    return out
 
 
def getRewards():
    if r.network=="Karura":
        url = 'wss://karura.polkawallet.io'
    else:
        url = 'wss://acala-rpc-0.aca-api.network'
      
    substrate = SubstrateInterface(url)
    hash = substrate.get_block_hash(block_id)
    timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
  
    data = []
    rewards = substrate.query_map(module='Rewards',storage_function='PoolInfos', block_hash = hash)
    for res in rewards:
        if res[0][0] == 'Dex':
            pair = res[0].value['Dex']['DexShare']
            lp = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'DexShare': pair}], block_hash = hash)
            legs = substrate.query(module='Dex',storage_function='LiquidityPool', params = [pair], block_hash = hash)
            outi = {"pair0": str(pair[0]), "pair1": str(pair[1]), "lp": str(lp.value), "leg0": str(legs[0].value), "leg1": str(legs[1].value)}
            data.append(outi)
    
    out = pd.DataFrame(data)
    return out

def getTotalIssuanceDOT():
    url = 'wss://acala-rpc-0.aca-api.network'
    substrate = SubstrateInterface(url)
    hash = substrate.get_block_hash(block_id)
    timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
  
    # Total Issuance data
    dot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'DOT'}], block_hash = hash)
    ldot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LDOT'}], block_hash = hash)
    lcdot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'LiquidCrowdloan': 13}], block_hash = hash)
    
    # homa = substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)
    try:
        homa = substrate.query_map(module='Homa',storage_function='StakingLedgers', block_hash = hash)
        amount = 0
        for res in homa:
            amount += res[1].value['bonded']
            
        # LKSM to KSM ratio
        ratio = ldot.value / amount 

    except:
        amount = 0
        # LDOT to DOT ratio
        ratio = 10
    
    # Build price DataFrame
    tmp = []
    p = substrate.query_map('AcalaOracle', 'Values', block_hash = hash)
    outi = {"id": {'Token': 'AUSD'}, "price": 1}
    tmp.append(outi)
    for res in p:
      outi = {"id": res[0].value, "price": res[1].value['value'] / 10**18}
      tmp.append(outi)
      if res[0].value=={'Token': 'DOT'}:
        outi = {"id": {'LiquidCrowdloan': 13}, "price": res[1].value['value'] / 10**18 / 1.4}
        tmp.append(outi)
        outi = {"id": {'Token': 'LDOT'}, "price": res[1].value['value'] / 10**18 / ratio}
        tmp.append(outi)
  
    # Price lookup function
    p = substrate.query('AcalaOracle', 'Values', params = [{'Token': 'DOT'}], block_hash = hash)
    price = p.value['value'] / 10**18
    
    # Decimals lookup function
    tmp = [{'id': {'Token': 'ACA'}, 'decimals': 12}, {'id': {'Token': 'DOT'}, 'decimals': 10}, {'id': {'Token': 'LDOT'}, 'decimals': 10}, {'id': {'LiquidCrowdloan': 13}, 'decimals': 10}, {'id': {'Token': 'AUSD'}, 'decimals': 12}]
    decimals = pd.DataFrame(tmp)
    def getDecimals(token):
      return int(decimals[decimals['id'] == token]['decimals'])        
  
    bridge = (float(dot.value / 10**10) + float(ldot.value / 10**10 / ratio)) * price
    liquidDOT = float(amount / 10**10) * price
    lcDOT = float(lcdot.value / 10**10) * price
    crowdloanTVL = (325159802323576263 / 10**10) * price
    return [bridge, liquidDOT, crowdloanTVL, lcDOT, block_id, ratio]

```



```{r tvl, cache = TRUE, include=FALSE}

# dailyTVL[, M := month(Date) %+% year(Date)]
# dailyTVL[, maxDate := max(Date), by = M]
# dailyTVL <- dailyTVL[Date == maxDate]

# LP Token Staking TVL = totalDexTvl = useTotalDexPoolTVL()
# totalDexLockedTvl = useTotalStaking() = api.query.dex?.liquidityPool
# KSM Bridge TVL = totalIssuanceKSM = useTotalIssuance('KSM') = api.query.tokens?.totalIssuance
dailyTVL[, bridgeTVL := 0]
# Liquid KSM TVL = totalLocked in Homa = useState()
dailyTVL[, liquidStakingTVL := 0]
# homeLocked = totalStaking * price
dailyTVL[, crowdloanTVL := 0]
dailyTVL[, LPTokenStakingTVL := 0]
if (tolower(network) == "acala") dailyTVL[, lcdotTVL := 0]

for (i in 1:nrow(dailyTVL)) {
  py_run_string("block_id = " %+% dailyTVL$Block[i])
  if (tolower(network) == "karura") {
    tmp = py$getTotalIssuanceKSM()
  } else {
    tmp = py$getTotalIssuanceDOT()
    dailyTVL$lcdotTVL[i] <- tmp[4]
  }
  dailyTVL$bridgeTVL[i] <- tmp[1]
  dailyTVL$liquidStakingTVL[i] <- tmp[2]
  dailyTVL$crowdloanTVL[i] <- tmp[3]
  
  # LPTokenStakingTVL 
  rewards1 <- py$getRewards()
  rewards <- as.data.table(rewards1)
  # sort(unique(c(rewards$pair0, rewards$pair1)))
  rewards[, pair0 := fixToken(pair0)]
  rewards[, pair1 := fixToken(pair1)]
  rewards <- merge(rewards, tokens, by.x = 'pair0', by.y="Token")
  setnames(rewards, "decimals", "decimals0")
  rewards[, Name := NULL]
  rewards <- merge(rewards, tokens, by.x = 'pair1', by.y="Token")
  setnames(rewards, "decimals", "decimals1")
  rewards[, Name := NULL]
  rewards[, decimals0 := as.numeric(decimals0)]
  rewards[, decimals1 := as.numeric(decimals1)]
  rewards[, lpDecimals := 12]
  rewards[pair0=='DOT' & pair1=='LCDOT', lpDecimals := 10]
  rewards[, lp := as.numeric(lp) / 10**lpDecimals]
  rewards[, leg0 := as.numeric(leg0) / 10**decimals0]
  rewards[, leg1 := as.numeric(leg1) / 10**decimals1]
  
  price = py$getOraclePrices() %>%
    as.data.table
  price[, token := fixToken(token)]
  
  rewards <- merge(rewards, price, by.x = "pair0", by.y="token", all.x = TRUE)
  setnames(rewards, "price", "price0")
  rewards <- merge(rewards, price, by.x = "pair1", by.y="token", all.x = TRUE)
  setnames(rewards, "price", "price1")
  rewards[pair0 %in% c('KUSD', "AUSD"), price0 := 1]
  rewards[pair1 %in% c('KUSD', "AUSD"), price1 := 1]
  rewards[, tvl := leg0 * price0 + leg1 * price1]
  rewards[is.na(tvl), tvl := (leg0 * price0) * 2]
  rewards[is.na(tvl), tvl := (leg1 * price1) * 2]
  rewards[, lpPrice := tvl / lp]  
  dailyTVL$LPTokenStakingTVL[i] <- sum(rewards$tvl, na.rm = TRUE)

}



# stableCoinTvl
# const stableCoinTVL = useTotalLocked(); = api.query.loans?.totalPositions
collateral <- getLoansDailyCollateral_acala_loan(network, window, staging = FALSE) %>%
  setorder(Date, collateral.id)
stableCoinTvl <- collateral[, sum(depositVolumeUSD), by = Date] %>%
  setnames("V1", "stableCoinTVL")
dailyTVL <- merge(dailyTVL, stableCoinTvl, by = "Date", all.x = TRUE)

# If monthly, remove these fields
try(dailyTVL[, M := NULL])
try(dailyTVL[, maxDate := NULL])

# Make columns numeric
for (col in names(dailyTVL)[-1]) set(dailyTVL, j=col, value=as.numeric(dailyTVL[[col]])) 
if (tolower(network) == "acala") {
  dailyTVL[, OverallTVL := dexTVL + LPTokenStakingTVL + bridgeTVL + liquidStakingTVL + lcdotTVL + stableCoinTVL]
} else {
  dailyTVL[, OverallTVL := dexTVL + LPTokenStakingTVL + bridgeTVL + liquidStakingTVL + stableCoinTVL]
}

# merge history
history <- fread(network %+% "_TVL.csv")
history[, Date := as.Date(Date)]
mindate <- min(dailyTVL$Date)
dailyTVL <- rbind(history[Date < mindate], dailyTVL, use.names = TRUE)
fwrite(dailyTVL, file = network %+% "_TVL.csv")


```

# `r network` {.tabset}

Row
----

### Overall Total Value Locked (TVL)

* The Overall TVL in the table below does not includes the Crowdloan TVL, which also seems to be the case on the stats.`r network`.network page.

```{r k_tvl}

knitr::kable(dailyTVL, escape = FALSE, format.args = list(big.mark = ",")) %>%
  kable_styling()

```

Row
----

### Components of Total Value Locked

```{r k_plot1}

if (tolower(network) == "acala") {
  dat <- melt(data = dailyTVL[, .(Date, dexTVL, LPTokenStakingTVL, bridgeTVL, liquidStakingTVL, lcdotTVL, stableCoinTVL)], 
            id.vars = "Date",
            value.name = "TVL")
  cols <- c('dexTVL', 'LPTokenStakingTVL', 'bridgeTVL', 'liquidStakingTVL', 'LCDOTTVL', 'stableCoinTVL')
} else {
  dat <- melt(data = dailyTVL[, .(Date, dexTVL, LPTokenStakingTVL, bridgeTVL, liquidStakingTVL, stableCoinTVL)], 
            id.vars = "Date",
            value.name = "TVL")
  cols <- c('dexTVL', 'LPTokenStakingTVL', 'bridgeTVL', 'liquidStakingTVL', 'stableCoinTVL')
  
}
dat[, TVL := TVL / 1e6]
 
# Stacked
ggplot(dat, aes(fill=variable, y=TVL, x=Date)) + 
    geom_bar(position="stack", stat="identity") +
    ggtitle(label="Components of Total Value Locked") + 
    ylab("Totval Value Locked (in millions USD)")

```


Row
----

### Sources

Please click on the *Source Code* link at the top of the document to see the full source code.  Here is a summary:

#### dexTVL 

- sum of tvlUSD from the xx function in the [subscanr](https://github.com/rogerjbos/subscanr) package for R.  You can also use the [Karura-dex](https://explorer.subquery.network/subquery/AcalaNetwork/karura-dex) project hosted on Subquery Network with this query:
```
{ 
  dailyPools { 
    nodes { 
      timestamp token0 {id} token1 {id} feeRateUSD dailyTradeVolumeUSD totalTVL txCount updateAtBlock {id}  
    } 
  } 
}
```

#### LPTokenStakingTVL
- ACA (or KAR) obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Rewards',storage_function='PoolInfos', block_hash = hash)*
- mulitplied by ACA (or KAR) price for the specific block number

#### BridgeTVL
- KSM (or DOT) obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'KSM'}], block_hash = hash)*
- LKSM (or LDOT) obtained from function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LKSM'}], block_hash = hash)*
- both mulitplied by KSM (or DOT) price for the specific block number

#### liquidStakingTVL
- KSM (or DOT) obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)*
- mulitplied by KSM (or DOT) price for the specific block number

#### lcdotTVL (Acala only)
- LCDOT obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'LiquidCrowdloan': 13}], block_hash = hash)*
- mulitplied by DOT price for the specific block number

#### stableCoinTVL
- sum of depositVolumeUSD from the *getLoansDailyCollateral_acala_loan* function in the [subscanr](https://github.com/rogerjbos/subscanr) package for R.  You can also use the [Karura-loan]https://explorer.subquery.network/subquery/AcalaNetwork/karura-loan) project hosted on Subquery Network with this query:
```
{ 
  dailyCollaterals { 
    nodes { 
      collateral {id} depositAmount debitAmount depositVolumeUSD debitVolumeUSD
      depositChangedUSD debitChangedUSD debitExchangeRate timestamp txCount 
    } 
  } 
}
```